package icu.qx.qc.service.impl;

import java.util.List;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import icu.qx.common.constant.TableNameEnum;
import icu.qx.common.utils.CodeUtils;
import icu.qx.qc.domain.QcProQuality;
import icu.qx.qc.domain.QcQualityProject;
import icu.qx.qc.domain.QcQualityQproject;
import icu.qx.qc.domain.param.QcProQualityParam;
import icu.qx.qc.service.IQcQualityQprojectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import icu.qx.qc.mapper.QcProQualityMapper;
import icu.qx.qc.service.IQcProQualityService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 过程检验Service业务层处理
 *
 * @author yong
 * @date 2024-06-24
 */
@Service
public class QcProQualityServiceImpl implements IQcProQualityService {
    @Autowired
    private QcProQualityMapper qcProQualityMapper;

    @Autowired
    private IQcQualityQprojectService qcQualityQprojectService;

    @Autowired
    private CodeUtils codeUtils;

    /**
     * 查询过程检验
     *
     * @param reportCode 过程检验主键
     * @return 过程检验
     */
    @Override
    public QcProQuality selectQcProQualityByReportCode(String reportCode) {
        return qcProQualityMapper.selectQcProQualityByReportCode(reportCode);
    }

    /**
     * 通过 检验单编号 查询
     */
    @Override
    public QcProQuality selectQcProQualityByQcCode(String reportQcCode) {
        return qcProQualityMapper.selectQcProQualityByQcCode(reportQcCode);
    }

    /**
     * 查询过程检验列表
     *
     * @return 过程检验
     */
    @Override
    public List<QcProQuality> selectQcProQualityList(QcProQuality qcProQuality) {
        return qcProQualityMapper.selectQcProQualityList(qcProQuality);
    }

    /**
     * 新增过程检验
     *
     * @return 结果
     */
    @Transactional(rollbackFor = {Exception.class, RuntimeException.class})
    @Override
    public int insertQcProQuality(QcProQualityParam qcProQualityParam) {
        String code = codeUtils.generateCode(TableNameEnum.QC_PRO_QUALITY.getTableName());
        qcProQualityParam.setReportQcCode(code);
        if (qcProQualityParam.getSelectedProjectList() != null && qcProQualityParam.getSelectedProjectList().size() > 0) {
            List<QcQualityProject> selectedProjectList = qcProQualityParam.getSelectedProjectList();
            selectedProjectList.forEach(e -> {
                e.getDrawbackList().forEach(e1 -> {
                    QcQualityQproject qcQualityQproject = new QcQualityQproject();
                    qcQualityQproject.setQqQualityCode(e.getQualityCode());
                    qcQualityQproject.setQqProCheckCode(code);
                    qcQualityQproject.setQqDrawbackCode(e1.getDrawbackCode());
                    qcQualityQprojectService.insertQcQualityQproject(qcQualityQproject);
                });
            });
        }
        return qcProQualityMapper.insertQcProQuality(qcProQualityParam);
    }

    /**
     * 修改过程检验
     *
     * @return 结果
     */
    @Transactional(rollbackFor = {Exception.class, RuntimeException.class})
    @Override
    public int updateQcProQuality(QcProQualityParam qcProQualityParam) {
        String code = qcProQualityParam.getReportQcCode();
        qcProQualityParam.setUpdateBy(SecurityUtils.getUsername());
        qcProQualityParam.setUpdateTime(DateUtils.getNowDate());
        // 删除所有
        qcQualityQprojectService.deleteQcQualityQprojectByQqProCheckCode(code);
        if (qcProQualityParam.getSelectedProjectList() != null && qcProQualityParam.getSelectedProjectList().size() > 0) {
            List<QcQualityProject> selectedProjectList = qcProQualityParam.getSelectedProjectList();
            selectedProjectList.forEach(e -> {
                e.getDrawbackList().forEach(e1 -> {
                    QcQualityQproject qcQualityQproject = new QcQualityQproject();
                    qcQualityQproject.setQqQualityCode(e.getQualityCode());
                    qcQualityQproject.setQqProCheckCode(code);
                    qcQualityQproject.setQqDrawbackCode(e1.getDrawbackCode());
                    qcQualityQprojectService.insertQcQualityQproject(qcQualityQproject);
                });
            });
        }
        return qcProQualityMapper.updateQcProQuality(qcProQualityParam);
    }

    /**
     * 批量删除过程检验
     *
     * @param reportCodes 需要删除的过程检验主键
     * @return 结果
     */
    @Override
    public int deleteQcProQualityByReportCodes(String[] reportCodes) {
        return qcProQualityMapper.deleteQcProQualityByReportCodes(reportCodes);
    }

    /**
     * 删除过程检验信息
     *
     * @param reportCode 过程检验主键
     * @return 结果
     */
    @Override
    public int deleteQcProQualityByReportCode(String reportCode) {
        return qcProQualityMapper.deleteQcProQualityByReportCode(reportCode);
    }
}
